const app = getApp();
const { getUserInfoWidthCredentials } = require('../../utils/ws/wsxcx-opendata-sdk');

Page({
  onLoad() {
    this.setData({ moduleName: app.modules || '' });
  },

  onUnload() {
    this.data.moduleName === 'ws_micro_shop' && wx.setStorageSync('cancel', true);
  },
  getinfo(e) {
    wx.showLoading();
    const that = this;
    let { moduleName } = this.data;
    let userInfo = wx.getStorageSync('user_info');

    if (e.detail.errMsg === 'getUserInfo:ok') {
      moduleName === 'ws_micro_shop' && this.shopModuleRequest(e).then(() => {
        userInfo.info = 1;
        wx.setStorageSync('user_info', userInfo);
        wx.setStorageSync('needAuthLoginOnPageLoad', false);
      }).then(() => { that.authDone() });

      moduleName === 'ws_micro_shop' || getUserInfoWidthCredentials(e).then(res => {
        if (Number(res.data.errno) === 0 && res.data.data) {
          wx.setStorageSync('_wxInfo', res.data.data);
          wx.setStorageSync('needAuthLoginOnPageLoad', false);
        }
      }).then(() => { that.authDone() });
    } else {
      this.authDone();
    }
  },

  authDone() {
    wx.hideLoading();
    app.util.navigateBack({ delta: 1 });
  },

  shopModuleRequest(e) {
    return new Promise(resolve => {
        app.ws_micro_shoprequest({
          url: app.api.passport.info,
          data: {
            user_info: JSON.stringify(e.detail.userInfo),
          },
          method: 'POST',
          success: function (res) {
            if (res.code === 0) {
            resolve();
            }
          },
        });
            });
  },
});